﻿using EntityModel;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;

namespace EGS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class NewStudentController : ControllerBase
    {
        private readonly EGSDbContext db;
        public NewStudentController(EGSDbContext context)
        {
            db = context;
        }

        public List<EGS_NewStudent> GetAll()
        {
            return db.NewStudent.ToList();
        }

        [HttpGet]
        public List<EGS_NewStudent> GetAll([FromBody] string Name, string ProfessionalName, string TeacherRecommendation)
        {
            IQueryable<EGS_NewStudent> query = db.NewStudent;

            if (!string.IsNullOrEmpty(Name))
            {
                query = query.Where(n => n.Name == Name);
            }
            if (!string.IsNullOrEmpty(ProfessionalName))
            {
                query = query.Where(n => n.ProfessionalName == ProfessionalName);
            }
            if (!string.IsNullOrEmpty(TeacherRecommendation))
            {
                if (!string.IsNullOrEmpty(TeacherRecommendation))
                    query = query.Where(n => n.TeacherRecommendation == TeacherRecommendation);
            }
            return query.ToList();
        }
        [HttpGet]
        public List<EGS_NewStudent> GetBySelect([FromBody] string Name, string ProfessionalName, string TeacherRecommendation)
        {
            List<EGS_NewStudent> newStudent = new List<EGS_NewStudent>();
            if (!string.IsNullOrEmpty(Name))
            {
                newStudent = db.NewStudent.Where(n => n.Name == Name).ToList();
            }
            if (!string.IsNullOrEmpty(ProfessionalName))
            {
                newStudent = db.NewStudent.Where(n => n.ProfessionalName == ProfessionalName).ToList();
            }
            if (!string.IsNullOrEmpty(TeacherRecommendation))
            {
                newStudent = db.NewStudent.Where(n => n.TeacherRecommendation == TeacherRecommendation).ToList();
            }
            return newStudent;
        }
        [HttpPost]
        public Result AddNewStudent([FromBody] EGS_NewStudent newStudent)
        {
            Result result = new Result();
            db.NewStudent.Add(newStudent);
            result.Data = db.SaveChanges();
            return result;
        }
        /// <summary>
        /// 改
        /// </summary>
        /// <param name="newStudent"></param>
        /// <returns></returns>
        [HttpPut]
        public bool Update(EGS_NewStudent newStudent)
        {
            db.NewStudent.Update(newStudent);
            int result = db.SaveChanges();
            return result > 0 ? true : false;
        }
        [HttpDelete]
        public Result DelNewStudent(Guid Id)
        {
            Result? result = new Result();
            EGS_NewStudent? newStudent = new EGS_NewStudent();
            newStudent = db.NewStudent.Where(n => n.ID == Id).FirstOrDefault();
            if(newStudent != null)
            {
                db.NewStudent.Update(newStudent);
                result.Data = db.SaveChanges();
            }
            return result;
        }
        //[HttpGet]
        //public Result GetNewStudent(Guid id)
        //{
        //    Result result = new Result();
        //    result.Data = db.NewStudent.Where(n => n.ID == id).FirstOrDefault();

        //    return result;
        //}

        [HttpGet("{id}")]
        public EGS_NewStudent? GetEGS_LoginInfoById(Guid id)
        {
            EGS_NewStudent? newStudent = new EGS_NewStudent();
            newStudent = db.NewStudent.Where(n => n.ID == id).FirstOrDefault();
            return newStudent;
        }
        /// <summary>
        /// 模糊查询名字
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        //[HttpGet("like/{name}")]
        //public List<EGS_NewStudent> GetEGS_NewStudentLikeName(string name)
        //{
        //    List<EGS_NewStudent> newStudent = new List<EGS_NewStudent>();
        //    newStudent = db.NewStudent.Where(n => n.Name.Contains(name)).ToList();
        //    return newStudent;
        //}
    }
}
